<?php
//------------------------------------------------------------------------------
// Class              : Ctl_Main
// Modification       :
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2007.07.02       ThanhTX                         Created
//------------------------------------------------------------------------------
class Ctl_Main extends ControlBase{
    var $modul="info";
	//==========================================================================
	// Function    : Ctl_Main
	// Creator     : ThanhTX 2007.07.02
	// Details     : Call the constructor of father class to initial variable
	//==========================================================================
	function Ctl_Main(){
         // Constructor of father class.
	   	 ControlBase::ControlBase();
	}

	//==========================================================================
	// Function    : execute
	// Creator     : ThanhTX 2007.07.02
	// Details     : Execute the process depend the given mode,
    //               as: Add, Delete, Edit and default is List.
	//==========================================================================
 	function execute($action=""){
	    global $db;
	    global     $arrayError,$_POST;
	    $arrayData =  array();
        // Get back the work you want to do.
	    $this->modul = CM_getParameter(MODUL);
 		if ($action==""){
            // Get back the action: as: Add, Delete, Edit or List.
 			$action = CM_getParameter(ACTION);
 		}
		switch($this->modul){
			case "baokim":
				include(DIR_CONTROL."ctl_baokim.class.php");
				$objbaokim = new Ctl_BaoKim($db);
				$objbaokim->execute("")  ;
				break;
			case "giohang":
				include(DIR_CONTROL."ctl_giohang.class.php");
				$objGiohang = new Ctl_Giohang($db);
				$objGiohang->execute("")  ;
				break;
			case "gopY":
				include(DIR_CONTROL."ctl_gop_y.class.php");
				$objgopY = new Ctl_GopY($db);
				$objgopY->execute("")  ;
				break;
			case "quangcao":
				include(DIR_CONTROL."ctl_quang_cao.class.php");
				$objadvertise = new Ctl_Advertise($db);
				$objadvertise->execute("")  ;
				break;
			case "gioithieu":
				include_once(DIR_CONTROL."ctl_gioi_thieu.class.php");
				$objInfo = new Ctl_Info($db);
				$objInfo->execute("");
				break;
			case "download":
				include_once(DIR_CONTROL."ctl_download_bao_gia.class.php");
				$objDownload = new Ctl_Download($db);
				$objDownload->execute("");
				break;
			case "chinhsach":
				include_once(DIR_CONTROL."ctl_chinh_sach.class.php");
				$objChinhsach = new Ctl_Chinhsach($db);
				$objChinhsach->execute("");
				break;
				
			case "tintuc":
				include_once(DIR_CONTROL."ctl_tin_tuc.class.php");
				$objTintuc = new Ctl_Tintuc($db);
				$objTintuc->execute("");
				break;
			case "tuyendung":
				include_once(DIR_CONTROL."ctl_tuyen_dung.class.php");
				$objTuyendung = new Ctl_Tuyendung($db);
				$objTuyendung->execute("");
				break;
			case "sanpham":
				include_once(DIR_CONTROL."ctl_san_pham.class.php");
				$objSanpham = new Ctl_Sanpham($db);
				$objSanpham->execute("");
				break;
			case "huongdan":
				include_once(DIR_CONTROL."ctl_huong_dan.class.php");
				$objHuongdan = new Ctl_Huongdan($db);
				$objHuongdan->execute("");
				break;
			case "sanphamdactrung":
				include_once(DIR_CONTROL."ctl_san_pham_dac_trung.class.php");
				$objSanphamdactrung = new Ctl_Sanphamdactrung($db);
				$objSanphamdactrung->execute("");
				break;
			
			case "content":
				include(DIR_CONTROL."ctl_content.class.php");
				$objcontent = new Ctl_content($db);
				$objcontent->execute("")  ;
				break;
			
			case "settingWeb":
				include(DIR_CONTROL."ctl_setting_web.class.php");
				$objsettingWeb = new Ctl_SettingWeb($db);
				$objsettingWeb->execute("")  ;
				break;
			case "lienHe":
				
				include(DIR_CONTROL."ctl_lien_he.class.php");
				$objlienHe = new Ctl_LienHe($db);
				$objlienHe->execute("")  ;
				break;
			case "home":
			default :{
				include_once(DIR_CONTROL."ctl_home.class.php");
				$objHome = new Ctl_Home($db);
				$objHome->execute("");
				break;
			}
		}
	}
	
	
	//==========================================================================
	// Function    : thamDo
	// Creator     : NgocVQ 2007.03.18
	// Details     : Display the data from tham_do and lua_chon_tham_do to screen.
	//==========================================================================
 	function thamDo(){
        $arrayDataThamDo = array();
        $arrayDataLuaChon = array();

        // Get a record of table loai_download with hien_thi = 1
        include_once(DIR_MODEL."md_tham_do_y_kien.class.php");
		
        $objModelThamDo = new MD_ThamDoYKien($this->db);
        $arrayDataThamDo = $objModelThamDo->getRecHienThi();
		
        if ($arrayDataThamDo != null){
            // Get tham do lua chon list follow chu_de
			$arrayDataLuaChon = $this->getLuaChonListFollowIDChuDe($arrayDataThamDo["id"]);
			if ($arrayDataLuaChon != null){

 	            // Display data to the screen
  		        include_once(DIR_VIEW."v_tham_do_y_kien.php");
    	        V_ThamDoYKien($arrayDataThamDo, $arrayDataLuaChon);
            }
        }

        unset($arrayDataThamDo);
        unset($arrayDataLuaChon);
    }
	 //==========================================================================
    // Function    : getChuDeRecordFollowID
    // Creator     : NgocVQ 2007.03.18
    // Details     : Get back the record from tham_do table with id = $chude
    //==========================================================================
     function getChuDeRecordFollowID($id_chude){
        global $db, $arrayError,$_POST;
        $arrayDataThamDo = array();

        include_once(DIR_MODEL."md_tham_do_y_kien.class.php");
        $objModelThamDo = new MD_ThamDoYKien($this->db);
        $arrayDataThamDo = $objModelThamDo->getRecByID($id_chude);
        /*if(empty($arrayDataThamDo)){
            $arrayError[]="Không tìm thấy bản ghi nào có id chủ đề = $id_chude.";
        }*/
        return $arrayDataThamDo;
     }

     //==========================================================================
    // Function    : getLuaChonListFollowIDChuDe
    // Creator     : NgocVQ 2007.03.18
    // Details     : Get tham do lua chon list follow chu_de
    //==========================================================================
     function getLuaChonListFollowIDChuDe($id_chude){
        global $db, $arrayError,$_POST;
        $arrayDataLuaChon = array();

        include_once(DIR_MODEL."md_tham_do_lua_chon.class.php");
        $objModelLuaChon = new MD_LuaChonThamDo($this->db);
        $arrayDataLuaChon = $objModelLuaChon->getList_Follow_Chu_De($id_chude);
        //if(empty($arrayDataLuaChon)){
        //    $arrayError[]="Không có lựa chọn trả lời nào.";
        //}
        return $arrayDataLuaChon;
     }
	//==========================================================================
	// Function    : info
	// Creator     : Thanhtx 2007.07.16
	// Details     : AppendData record of table
	//==========================================================================
	function info(){
		include_once(DIR_MODEL."md_info.class.php");
		global $db;
		$objinfo = new MD_Info($db);
		$arrayData_Info =  array();      
		$arrayData_Info = $objinfo->getByWhere("id,tieu_de","hien_thi=1","id asc"); 
		
		if (!empty($arrayData_Info)) { 
			showInfoForUser($arrayData_Info);  
			
		unset($arrayData_Info);
		}
	}
	//==========================================================================
	// Function    : info
	// Creator     : Thanhtx 2007.07.16
	// Details     : AppendData record of table
	//==========================================================================
	function product(){
		include_once(DIR_MODEL."md_product.class.php");
		global $db;
		$objproduct = new MD_Product($db);
		$arrayData_Product =  array();      
		$arrayData_Product = $objproduct->getByWhere("id,tieu_de","hien_thi=1","id asc"); 
		
		if (!empty($arrayData_Product)) { 
			showProductForUser($arrayData_Product);  
			
		unset($arrayData_Product);
		}
	}
	//==========================================================================
	// Function    : tinNoiBat
	// Creator     : ThanhTX 2007.07.03
	// Details     : AppendData record of table
	//==========================================================================

 	function advertise(){
		global $db;
		global     $arrayError,$_POST;
		//lay nguon ho cat advertise
		
		$arrayData =  array();
		include_once(DIR_MODEL."md_quang_cao.class.php");
		$objAdvertise = new MD_Quangcao($this->db);
		$arrayData = $objAdvertise->getByWhere("*","","");
		// show data
		include_once(DIR_VIEW."v_quang_cao_list.php");
		//check loi
		if ($objAdvertise->errno())
		{
			  $arrayError[]= $objAdvertise->error();
		}
		V_AdvertiseList($arrayData);
		unset($arrayData);
			
		
	}
 	function counterAccess(){
		global $db;
		global     $arrayError;
		$arayData=array();
		include_once(DIR_MODEL."md_counter_access.class.php");
		$objModel = new MD_CounterAccess($this->db);
		if(!session_is_registered("ssDaDem")){
			$objModel->update($arayData);
			session_register("ssDaDem");
		}
		$arrayData = $objModel->getAllDataView("count_access");
		return (number_format( $arrayData['count_access'],0,',','.'));
		
	}
	
	
	//==========================================================================
	// Function    : contentNews
	// Creator     : ThanhTX 2007.07.03
	// Details     : AppendData record of table
	//==========================================================================

 	function contentNews(){
		global $db;
	    global     $arrayError,$_POST;
	    $arrayData =  array();
		include_once(DIR_MODEL."md_content.class.php");
		$objContent = new MD_Content($db);
		//$arrayData = $objContent->getContentNews("id,title,introtext",NUM_NEW_LINK);
		$arrayData = $objContent->getByWhereLimit("*","shows=1","id desc","15");
		 // show data
		include_once(DIR_VIEW."v_content_news.php");
		//check loi
		if ($objContent->errno()){
				 $arrayError[]= $objContent->error();
		}
		V_ContentNewsShow($arrayData);
		unset($arrayData);
	}
	//==========================================================================
	// Function    : counterOnline
	// Creator     : Tran Thanh 2007.07.14
	// Details     : AppendData record of table
	//==========================================================================
 	function counterOnline(){
		global $db;
		global     $arrayError;
		include_once(DIR_MODEL."md_counter_online.class.php");
		$objModel = new MD_CounterOnline($this->db);
		include_once(DIR_MODEL."md_counter_online.class.php");
		$objCounterOnline = new MD_CounterOnline($db);
		$arayData=array("session_id"=>session_id(),
						"activity"=>"now()",
						"ip_address"=>"{$_SERVER['REMOTE_ADDR']}",
						"refurl"=>"{$_SERVER['HTTP_REFERER']}",
						"user_agent"=>"{$_SERVER['HTTP_USER_AGENT']}",
						"member"=>"y");
		if(!session_is_registered('online')){
			$objCounterOnline->insert($arayData);
			session_register('online');
		}else{
			 if(session_is_registered('user_id')){
			 	$objCounterOnline->update1($arayData);
			 }
		}
		if(session_is_registered('online')){
			$objCounterOnline->update2($arayData);
		}
		//check loi
		if ($objCounterOnline->errno()){
             $arrayError[]= $objCounterOnline->error();
             return false;
     	}
		//lay ra so ngươi dang online
		$limit_time = time() - 300; // 5 Minute time out. 60 * 5 = 300
		 $arrayData = $objModel->getByWhereGroup("session_id,activity,ip_address,refurl,user_agent","UNIX_TIMESTAMP(activity) >= $limit_time AND member='n'","ip_address");
		return count($arrayData);
		//thc hien xoa
		$maxtime = time() -600;
		$objCounterOnline->deleteWhrere("UNIX_TIMESTAMP(activity) < '$maxtime'");
		if ($objCounterOnline->errno()){
             $arrayError[]= $objCounterOnline->error();
             return false;
     	}
	}
	
}
?>
